package net.mihome.app.ui.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.tencent.mm.sdk.platformtools.Log;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.adapter.ListViewDianpuAdapter;
import net.mihome.app.bean.Article;
import net.mihome.app.bean.Shop;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.maxwin.view.XListView;

public class MifuwuDianpuListView extends XListView {
	private Handler handler;
	private ListViewDianpuAdapter adapter;
	private List<Shop> listData = new ArrayList<Shop>();
	private AppContext appContext;
	private int begin=0;
	private int num=10;
	private int newsTypeId=Article.ARTICLE_TYPE_BANZHENG;
	private MyViewLinster myViewLinster;
	public String titleSearchString=null;
    private int type=-1;
    private Integer showType=null;
	public MifuwuDianpuListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread arg0, Throwable arg1) {
				// TODO Auto-generated method stub
				arg1.printStackTrace();
			}
		});
        afterInit();
		// TODO Auto-generated constructor stub
	}
	public MifuwuDianpuListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
        afterInit();
		// TODO Auto-generated constructor stub
	}
	private void initData(int shopType){
        this.type=shopType;
        initData();
    }
	private void afterInit(){
		adapter = new ListViewDianpuAdapter(getContext(),R.layout.active_listitem, listData);
		setAdapter(adapter);
        setPullLoadEnable(true);
        setPullRefreshEnable(true);
		appContext=(AppContext) getContext().getApplicationContext();
		setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
                if(myViewLinster!=null){
                    myViewLinster.onBeforeLoad();
                }
                begin=0;
                num=10;
                RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
                requestParams.add("begin",begin+"");
                requestParams.add("num",num+"");
                if(showType!=null){
                    requestParams.put("show_type",showType);
                }
                appContext.ASYNC_HTTP_CLIENT.get(URLs.MIHOME_GET_SHOP_LIST, requestParams, new MiAsyncHttpResponseHandler<List<Map>>() {
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, ResponsePojo<List<Map>> responsePojo) {
                        List<Shop> list = Shop.parserList(responsePojo);
                        if (list != null && !list.isEmpty()) {
                            listData.clear();
                            listData.addAll(list);
                            adapter.notifyDataSetChanged();
                            if (myViewLinster != null) {
                                myViewLinster.onLoadComplete();
                            }
                        }
                        stopRefresh();
                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                        if (myViewLinster != null) {
                            myViewLinster.onLoadComplete();
                        }
                        Toast.makeText(appContext, arg3.getMessage(), Toast.LENGTH_SHORT).show();
                        stopRefresh();
                    }
                });
			}

			@Override
			public void onLoadMore() {
				begin=begin+num;
                RequestParams requestParams=RequestParamsFactory.newInstance(AppContext.getInstance());
                requestParams.add("begin",begin+"");
                requestParams.add("num",num+"");
                if(showType!=null){
                    requestParams.put("show_type",showType);
                }
                appContext.ASYNC_HTTP_CLIENT.get(URLs.MIHOME_GET_SHOP_LIST, requestParams, new MiAsyncHttpResponseHandler<List<Map>>() {
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, ResponsePojo<List<Map>> responsePojo) {
                        List<Shop> list = Shop.parserList(responsePojo);
                        if (list != null && !list.isEmpty()) {
                            listData.addAll(list);
                            adapter.notifyDataSetChanged();
                            if (myViewLinster != null) {
                                myViewLinster.onLoadComplete();
                            }
                        }
                        if(myViewLinster!=null){
                            myViewLinster.onLoadComplete();
                        }
                        stopLoadMore();
                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                        if (myViewLinster != null) {
                            myViewLinster.onLoadComplete();
                        }
                        Toast.makeText(appContext, arg3.getMessage(), Toast.LENGTH_SHORT).show();
                        stopLoadMore();
                    }
                });
			}
		});

        setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Shop shop = (Shop) parent.getAdapter().getItem(position);
                UIHelper.showShopRedirect(view.getContext(), shop);
            }
        });
	}
	public void initData(){
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
        startRefresh();
	}
	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}
	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}

    public Integer getShowType() {
        return showType;
    }

    public void setShowType(Integer showType) {
        this.showType = showType;
    }
}
